package com.fuxi.cloud.common.util;

import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectResult;
import org.apache.tomcat.util.http.fileupload.FileItemStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Date;
import java.util.UUID;

/**
 * OssBootUtil
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-05-03
 */
public class OssBootUtil {
    private static final Logger log = LoggerFactory.getLogger(OssBootUtil.class);
    private static String endPoint;
    private static String accessKeyId;
    private static String accessKeySecret;
    private static String bucketName;
    private static String staticDomain;
    private static OSSClient ossClient = null;

    public OssBootUtil() {
    }

    public static void setEndPoint(String endPoint) {
        endPoint = endPoint;
    }

    public static void setAccessKeyId(String accessKeyId) {
        accessKeyId = accessKeyId;
    }

    public static void setAccessKeySecret(String accessKeySecret) {
        accessKeySecret = accessKeySecret;
    }

    public static void setBucketName(String bucketName) {
        bucketName = bucketName;
    }

    public static void setStaticDomain(String staticDomain) {
        staticDomain = staticDomain;
    }

    public static String getStaticDomain() {
        return staticDomain;
    }

    public static String getEndPoint() {
        return endPoint;
    }

    public static String getAccessKeyId() {
        return accessKeyId;
    }

    public static String getAccessKeySecret() {
        return accessKeySecret;
    }

    public static String getBucketName() {
        return bucketName;
    }

    public static OSSClient getOssClient() {
        return ossClient;
    }

    public static String upload(MultipartFile file, String fileDir, String customBucket) {
        String var3 = null;
        initOSS(endPoint, accessKeyId, accessKeySecret);
        StringBuilder var4 = new StringBuilder();
        String var5 = bucketName;
        if (oConvertUtils.isNotEmpty(customBucket)) {
            var5 = customBucket;
        }

        try {
            if (!ossClient.doesBucketExist(var5)) {
                ossClient.createBucket(var5);
            }

            String var6 = file.getOriginalFilename();
            if ("" == var6) {
                var6 = file.getName();
            }

            var6 = CommonUtils.getFileName(var6);
            String var7 = var6.substring(0, var6.lastIndexOf(".")) + "_" + System.currentTimeMillis() + var6.substring(var6.indexOf("."));
            if (!fileDir.endsWith("/")) {
                fileDir = fileDir.concat("/");
            }

            fileDir = CommonUtils.filter(fileDir);
            var4 = var4.append(fileDir + var7);
            if (oConvertUtils.isNotEmpty(staticDomain) && staticDomain.toLowerCase().startsWith("http")) {
                var3 = staticDomain + "/" + var4;
            } else {
                var3 = "https://" + var5 + "." + endPoint + "/" + var4;
            }

            PutObjectResult var8 = ossClient.putObject(var5, var4.toString(), file.getInputStream());
            if (var8 != null) {
                log.debug("------OSS文件上传成功------" + var4);
            }

            return var3;
        } catch (IOException var9) {
            var9.printStackTrace();
            return null;
        }
    }

    public static String upload(MultipartFile file, String fileDir) {
        return upload(file, fileDir, (String)null);
    }

    public static String upload(FileItemStream file, String fileDir) {
        String var2 = null;
        initOSS(endPoint, accessKeyId, accessKeySecret);
        StringBuilder var3 = new StringBuilder();

        try {
            String var4 = file.getName().substring(file.getName().lastIndexOf(46));
            String var5 = UUID.randomUUID().toString().replace("-", "") + var4;
            if (!fileDir.endsWith("/")) {
                fileDir = fileDir.concat("/");
            }

            fileDir = CommonUtils.filter(fileDir);
            var3 = var3.append(fileDir + var5);
            if (oConvertUtils.isNotEmpty(staticDomain) && staticDomain.toLowerCase().startsWith("http")) {
                var2 = staticDomain + "/" + var3;
            } else {
                var2 = "https://" + bucketName + "." + endPoint + "/" + var3;
            }

            PutObjectResult var6 = ossClient.putObject(bucketName, var3.toString(), file.openStream());
            ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
            if (var6 != null) {
                log.debug("------OSS文件上传成功------" + var3);
            }

            return var2;
        } catch (IOException var7) {
            var7.printStackTrace();
            return null;
        }
    }

    public static void deleteUrl(String url) {
        deleteUrl(url, (String)null);
    }

    public static void deleteUrl(String url, String bucket) {
        String var2 = bucketName;
        if (oConvertUtils.isNotEmpty(bucket)) {
            var2 = bucket;
        }

        String var3 = "";
        if (oConvertUtils.isNotEmpty(staticDomain) && staticDomain.toLowerCase().startsWith("http")) {
            var3 = staticDomain + "/";
        } else {
            var3 = "https://" + var2 + "." + endPoint + "/";
        }

        url = url.replace(var3, "");
        ossClient.deleteObject(var2, url);
    }

    public static void delete(String fileName) {
        ossClient.deleteObject(bucketName, fileName);
    }

    public static InputStream getOssFile(String objectName, String bucket) {
        BufferedInputStream var2 = null;

        try {
            String var3 = bucketName;
            if (oConvertUtils.isNotEmpty(bucket)) {
                var3 = bucket;
            }

            initOSS(endPoint, accessKeyId, accessKeySecret);
            OSSObject var4 = ossClient.getObject(var3, objectName);
            var2 = new BufferedInputStream(var4.getObjectContent());
        } catch (Exception var5) {
            log.error("文件获取失败" + var5.getMessage());
        }

        return var2;
    }

    public static InputStream getOssFile(String objectName) {
        return getOssFile(objectName, (String)null);
    }

    public static String getObjectURL(String bucketName, String objectName, Date expires) {
        initOSS(endPoint, accessKeyId, accessKeySecret);

        try {
            if (ossClient.doesObjectExist(bucketName, objectName)) {
                URL var3 = ossClient.generatePresignedUrl(bucketName, objectName, expires);
                return URLDecoder.decode(var3.toString(), "UTF-8");
            }
        } catch (Exception var4) {
            log.error("文件路径获取失败" + var4.getMessage());
        }

        return null;
    }

    private static OSSClient initOSS(String endpoint, String accessKeyId, String accessKeySecret) {
        if (ossClient == null) {
            ossClient = new OSSClient(endpoint, new DefaultCredentialProvider(accessKeyId, accessKeySecret), new ClientConfiguration());
        }

        return ossClient;
    }

    public static String upload(InputStream stream, String relativePath) {
        String var2 = null;
        initOSS(endPoint, accessKeyId, accessKeySecret);
        if (oConvertUtils.isNotEmpty(staticDomain) && staticDomain.toLowerCase().startsWith("http")) {
            var2 = staticDomain + "/" + relativePath;
        } else {
            var2 = "https://" + bucketName + "." + endPoint + "/" + relativePath;
        }

        PutObjectResult var4 = ossClient.putObject(bucketName, relativePath.toString(), stream);
        ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
        if (var4 != null) {
            log.debug("------OSS文件上传成功------" + relativePath);
        }

        return var2;
    }
}

